﻿using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;

namespace Lizk.SimpleHUD
{
    /// <summary>
    /// Visits a HUD tree and fires events for every node visited.
    /// </summary>
    public class HUDVisitor
    {
        private Predicate<HUDControl> defaultPredicate = x => x.Visible && x.Alpha > 0 && x.Size != SizeS.Empty;
        public Predicate<HUDControl> DefaultPredicate { get { return defaultPredicate; } }
        public event EventHandler<VisitEventArgs> BeginControl, EndControl;
        
        public void Visit(HUDControl control)
        {
            Visit(control, defaultPredicate);
        }

        public void Visit(HUDControl control, Predicate<HUDControl> filter)
        {
            if (!filter(control))
                return;

            if (BeginControl != null)
                BeginControl(this, new VisitEventArgs(control));

            if (control is IContainer)
            {
                HUDContainer container = control as HUDContainer;
                foreach (var subControl in container)
                {
                    Visit(subControl);
                }
            }

            if (EndControl != null)
                EndControl(this, new VisitEventArgs(control));
        }

        public class VisitEventArgs : EventArgs
        {
            public HUDControl Control { get; set; }

            public VisitEventArgs(HUDControl control)
            {
                this.Control = control;
            }
        }

    }
}
